import javax.swing.*;

import java.awt.*;

public class SortView
{
	public JFrame frame;
	private JPanel mainPanel;
	private JPanel mainSortPanel;
	private JPanel sortPanel1;
	private JPanel sortPanel2;
	private JPanel startPanel;
	private JPanel bucketPanel;
	private JPanel controlPanel;
	private JPanel buttonPanel;
	private JPanel sliderPanel;
	private JPanel[] buckets;
	private JPanel[][] bucketItems;
	private JPanel[][] abacus;
	private JPanel[] startList;
	private JLabel[] bucketLabels;
	private boolean[][] bucketFlag;
	private boolean[] startFlag;
	public JButton BucketButton;
	public JButton BeadButton;
	public JButton BothButton;
	public JSlider speedSlider;
	
	public SortView(int numBuckets, int abacusHeight, int abacusWidth)
	{
		abacus = new JPanel[abacusHeight][abacusWidth];
		buckets = new JPanel[numBuckets];
		bucketItems = new JPanel[numBuckets][abacusHeight];
		startList = new JPanel[abacusHeight];
		bucketLabels = new JLabel[abacusHeight];
		bucketFlag = new boolean[numBuckets][abacusHeight];
		startFlag = new boolean[abacusHeight];
		
		for(int i = 0; i < abacus.length; i++)
		{
			for(int j = 0; j < abacus[0].length; j++)
			{
				abacus[i][j] = new JPanel();
				abacus[i][j].setBorder(BorderFactory.createLineBorder(Color.black));
			}
		}
		
		frame = new JFrame("Bucket Sort");
		frame.setSize(600, 500);
		
		mainSortPanel = new JPanel();
		mainSortPanel.setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.BOTH;
		c.weighty = 1;
		c.weightx = 1;
		
		sortPanel1 = new JPanel();
		sortPanel1.setLayout(new BoxLayout(sortPanel1, BoxLayout.Y_AXIS));
		sortPanel1.setBorder(BorderFactory.createLineBorder(Color.red));
		
		startPanel = new JPanel();
		startPanel.setBorder(BorderFactory.createLineBorder(Color.black));
		
		for(int i = 0; i < startList.length; i++)
		{
			startList[i] = new JPanel();
			startPanel.add(startList[i]);
		}
		
		bucketPanel = new JPanel();
		bucketPanel.setBorder(BorderFactory.createLineBorder(Color.black));
		bucketPanel.setLayout(new GridLayout(0, buckets.length));
		
		for(int i = buckets.length-1; i >= 0; i--)
		{
			buckets[i] = new JPanel();
			buckets[i].setBorder(BorderFactory.createLoweredBevelBorder());
			for(int n = 0; n < bucketItems[i].length; n++)
			{
				bucketItems[i][n] = new JPanel();
				buckets[i].add(bucketItems[i][n]);
				bucketFlag[i][n] = false;
			}
			bucketPanel.add(buckets[i]);
		}
		
		sortPanel1.add(startPanel);
		sortPanel1.add(bucketPanel);
		
		sortPanel2 = new JPanel();
		sortPanel2.setLayout(new GridLayout(abacus.length, abacus[0].length));
		sortPanel2.setBorder(BorderFactory.createLineBorder(Color.red));
		
		for(int i = 0; i < abacus.length; i++)
		{
			for(int j = 0; j < abacus[0].length; j++)
			{
				sortPanel2.add(abacus[i][j]);
			}
		}
		
		c.gridx = 0;
		c.gridy = 0;
		mainSortPanel.add(sortPanel1, c);
		c.gridx = 1;
		c.gridy = 0;
		mainSortPanel.add(sortPanel2, c);
		
		BucketButton = new JButton("Run Bucket Sort");
		BeadButton = new JButton("Run Bead Sort");
		BothButton = new JButton("Run Both Sorts");
		speedSlider = new JSlider(JSlider.HORIZONTAL, 0, 1000, 500);
		
		buttonPanel = new JPanel();
		buttonPanel.setLayout(new GridBagLayout());
		c.gridx = 0;
		c.gridy = 0;
		buttonPanel.add(BucketButton, c);
		c.gridx = 1;
		c.gridy = 0;
		buttonPanel.add(BeadButton, c);
		c.gridx = 0;
		c.gridy = 1;
		c.gridwidth = 2;
		buttonPanel.add(BothButton, c);
		
		buttonPanel.setSize(10, 10);
		
		sliderPanel = new JPanel();
		sliderPanel.setLayout(new BoxLayout(sliderPanel, BoxLayout.X_AXIS));
		sliderPanel.add(new JLabel("Slow"));
		sliderPanel.add(speedSlider);
		sliderPanel.add(new JLabel("Fast"));
		
		controlPanel = new JPanel();
		controlPanel.setLayout(new BoxLayout(controlPanel, BoxLayout.Y_AXIS));
		controlPanel.add(buttonPanel);
		controlPanel.add(sliderPanel);
		
		mainPanel = new JPanel();
		mainPanel.setLayout(new GridBagLayout());
		
		c.gridx = 0;
		c.gridy = 0;
		mainPanel.add(mainSortPanel, c);
		c.gridx = 0;
		c.gridy = 1;
		c.weighty = 0.1;
		mainPanel.add(controlPanel, c);
		
		frame.add(mainPanel);
		
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	public void updateBead(int beads[][])
	{
		for(int i = 0; i < abacus.length; i++)
		{
			for(int j = 0; j < abacus[0].length; j++)
			{
				if(beads[i][j] == 1)
				{
					abacus[i][j].setBorder(BorderFactory.createRaisedBevelBorder());
				}
				else
				{
					abacus[i][j].setBorder(BorderFactory.createLineBorder(Color.black));
				}
			}
		}
	}
	
	public void setBucketList(int[] list)
	{
		for(int i = 0; i < startList.length; i++)
		{
			bucketLabels[i] = new JLabel(Integer.toString(list[i]));
			startList[i].add(bucketLabels[i]);
			startList[i].setBorder(BorderFactory.createRaisedBevelBorder());
			startFlag[i] = true;
		}
		for(int i = 0; i < buckets.length; i++)
		{
			for(int n = 0; n < bucketItems[i].length; n++)
			{
				bucketItems[i][n].removeAll();
				bucketItems[i][n].setBorder(BorderFactory.createEmptyBorder());
				bucketFlag[i][n] = false;
			}
		}
	}
	
	public void updateBuckets(int number, int bucket)
	{
		for(int i = bucketItems[bucket].length-1; i >= 0; i--)
		{
			if(!bucketFlag[bucket][i])
			{
				for(int n = 0; n < startList.length; n++)
				{
					if(bucketLabels[i].getText().equals(Integer.toString(number)) && startFlag[i])
					{
						startList[i].removeAll();
						startList[i].setBorder(BorderFactory.createEmptyBorder());
						startFlag[i] = false;
						bucketItems[bucket][i].add(bucketLabels[i]);
						bucketItems[bucket][i].setBorder(BorderFactory.createRaisedBevelBorder());
						break;
					}
				}
				break;
			}
		}
	}
}